package org.yate.oa.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.yate.oa.model.HighVoltage;
import org.yate.oa.service.HighVoltageService;
import org.yate.oa.utils.Pagination;

import com.alibaba.fastjson.JSONObject;

@WebServlet(urlPatterns = { "/highVol/get", "/highVol/post", "/highVol/put", "/highVol/delete" }, asyncSupported = true)
public class HighVolController extends BaseServletController
{
    private HighVoltageService highVoltageService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.highVoltageService = this.springCtx.getBean(HighVoltageService.class);
    }
    
    protected void doDelete(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PrintWriter out = rsp.getWriter();
        JSONObject json = new JSONObject();
        highVoltageService.remove(Long.parseLong(id));
        json.put("code", "200");
        json.put("msg", "ok");
        out.println(json);
        out.flush();
        out.close();
    }
    
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        PrintWriter out = resp.getWriter();
        JSONObject json = new JSONObject();
        HighVoltage e = highVoltageService.getEntity(Long.parseLong(id));
        json.put("e", e);
        out.println(json);
        out.flush();
        out.close();
    }
    
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String submitType = req.getParameter("submitType");
        if(submitType.equals("list")){
            getHighVolList(req, resp);
        }else {
            String[] kva = req.getParameterValues("kva");
            String highVol = req.getParameter("highVol");
            String lowVol = req.getParameter("lowVol");
            String boxNum = req.getParameter("boxNum");
            String highVolConf = req.getParameter("highVolConf");
            String voltageConf = req.getParameter("voltageConf");
            
            HighVoltage e = new HighVoltage();
            e.setType(0);
            e.setKva(StringUtils.join(kva, ","));
            e.setHighVol(Integer.parseInt(highVol));
            e.setLowVol(Integer.parseInt(lowVol));
            e.setBoxNum(Integer.parseInt(boxNum));
            e.setHighVolConf(Integer.parseInt(highVolConf));
            e.setVoltageConf(Integer.parseInt(voltageConf));
            String detail = req.getParameter("detail");
            e.setDetail(detail);
            if(submitType.equals("add")) {
                highVoltageService.add(e);
            } else {
                String id = req.getParameter("id");
                e.setId(Long.parseLong(id));
                highVoltageService.update(e);
            }
          
            req.getRequestDispatcher("/jsp/highVol/list.jsp").forward(req, resp);
        }
    }
    
    private void getHighVolList(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        PrintWriter out = resp.getWriter();
        JSONObject json = new JSONObject();
        String page = req.getParameter("page");
        int pageIndex = 0;
        if (!StringUtils.isEmpty(page)) {
            pageIndex = Integer.parseInt(page);
        }
        HighVoltage e = new HighVoltage();
        e.setType(Integer.parseInt(req.getParameter("type")));
        Pagination<HighVoltage> data = highVoltageService.getEntities(pageIndex, 10, e, "", ""); 
        json.put("status", "200");
        json.put("data", data);
        out.println(json);
        out.flush();
        out.close();
    }
    
}
